package com.enpronomics.pcall.activity;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.enpronomics.pcall.MyLocation;
import com.enpronomics.pcall.MyLocation.LocationResult;
import com.enpronomics.pcall.ServiceManager;

public class PanicStartActivity extends Activity {

	// private String[] panicContacts = new String[5];
	private String panicSMSNumber;
	private String panicCallNumber;
	private String PositionReportNumber;
	private String messageType, name;
	private int repetition, interval;
	private boolean panicInitiated;

	private static TimerTask mTimerTask;
	private final Handler handler = new Handler();
	private Timer t = new Timer();
	private int nCounter = 0;

	// String SMSNumber;
	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		if (ServiceManager.getBoolSetting(this, ServiceManager.KEY_PANIC)) {
			panicSMSNumber = ServiceManager.getStringSetting(this,
					ServiceManager.KEY_PANIC_SMS_NUMBER);
			panicCallNumber = ServiceManager.getStringSetting(this,
					ServiceManager.KEY_PANIC_CALL_NUMBER);
			PositionReportNumber = ServiceManager.getStringSetting(this,
					ServiceManager.KEY_POSITION_REPORT_NUMBER);
			messageType = ServiceManager.getStringSetting(this,
					ServiceManager.MESSAGE_TYPE);
			name = ServiceManager.getStringSetting(this,
					ServiceManager.KEY_NAME);
			repetition = ServiceManager.getIntSetting(this,
					ServiceManager.KEY_SOS_REPETITION);
			interval = ServiceManager.getIntSetting(this,
					ServiceManager.KEY_ACTION_INTERVAL);
			panicInitiated = ServiceManager.getBoolSetting(this,
					ServiceManager.PANIC_INITIATED);

			if (!panicCallNumber.equals("None") && messageType.equals("Panic")) {
				if (!panicInitiated) {
					// SMSNumber = panicCallNumber;
					ServiceManager.setBoolSetting(this,
							ServiceManager.PANIC_INITIATED, true);
					ServiceManager.call(PanicStartActivity.this,
							panicCallNumber);
					doTimerTask();
					Toast.makeText(this, "Panic Initiated", Toast.LENGTH_LONG)
							.show();
					// new MyLocation().init(this, locationResult);
				} else {
					ServiceManager.setBoolSetting(this,
							ServiceManager.PANIC_INITIATED, false);
					if (mTimerTask != null) {
						mTimerTask.cancel();
						Toast.makeText(this, "Panic Stopped", Toast.LENGTH_LONG).show();
					}
				}
			} else if (!PositionReportNumber.equals("None")
					&& !messageType.equals("Panic")) {
				// SMSNumber = PositionReportNumber;
				new MyLocation().init(this, locationResult);
			}
		}
		finish();
	}

	public void doTimerTask() {

		mTimerTask = new TimerTask() {
			public void run() {
				handler.post(new Runnable() {
					public void run() {
						if (nCounter == repetition) {
							ServiceManager.setBoolSetting(
									PanicStartActivity.this,
									ServiceManager.PANIC_INITIATED, false);
							mTimerTask.cancel();
							Toast.makeText(PanicStartActivity.this,
									"Panic Finished", Toast.LENGTH_LONG).show();
						}

						else
							new MyLocation().init(PanicStartActivity.this,locationResult);
						// Log.d("TIMER", "TimerTask run");
						nCounter++;
					}
				});
			}
		};
		// public void schedule (TimerTask task, long delay, long period)
		t.schedule(mTimerTask, 1000 * 30, interval * 1000 * 60);
	}

	public final LocationResult locationResult = new LocationResult() {
		@Override
		public void gotLocation(final Location location) {
			String locationStr;
			if (location == null)
				locationStr = "No location Found";
			else
				locationStr = ServiceManager.getLocationStr(location);

			// long s = System.currentTimeMillis();
			// Date date = new Date(s);
			// String currentTimeString =
			// DateFormat.getDateTimeInstance(DateFormat.SHORT,
			// DateFormat.SHORT).format(date);
			String currentTimeString = ServiceManager.getCurrentDate();

			// for (int i = 0; i < panicContacts.length; i++) {
			if (messageType.equals("Panic")) {
				if (!panicSMSNumber.equals("None")) {
					ServiceManager.sendSMS(PanicStartActivity.this,
							panicSMSNumber, ServiceManager.PANIC_MESSAGE + ";"
									+ locationStr + ";" + name + ";"
									+ currentTimeString);
				}
			} else {
				if (!PositionReportNumber.equals("None")) {
					ServiceManager.sendSMS(PanicStartActivity.this,
							PositionReportNumber, ServiceManager.PANIC_MESSAGE
									+ ";" + locationStr + ";" + name + ";"
									+ currentTimeString);
				}
				ServiceManager.setStringSetting(PanicStartActivity.this,
						ServiceManager.MESSAGE_TYPE, "Panic");
			}
		}
	};
}
